package se.webbzon.boltzmann.game.object.dialogue.word;

import java.util.Collection;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.environment.PlayerEnvironment;
import se.webbzon.boltzmann.game.object.dialogue.AbstractDialogue;
import se.webbzon.boltzmann.game.object.dialogue.layout.DialogueLayout;
import se.webbzon.boltzmann.game.object.player.AbstractPlayerCharacter;
import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.builder.SoftSequenceBinding;
import se.webbzon.boltzmann.input.Button;
import se.webbzon.boltzmann.input.ButtonInfo;
import se.webbzon.boltzmann.player.Player;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;
import se.webbzon.oschi01.worldobject.WorldObject;

public class InputDialogueWord extends ImageDialogueWord {
	
	// The default image of this input dialogue word
	private static final ResourceSet<Texture3D> defaultImage = Resources.getTextureSet("keyboard_key");

	// The input to display
	private final String input;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public InputDialogueWord(DialogueLayout layout, String input) {
		super(layout, defaultImage);
		this.input = input.toUpperCase();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void onUpdateWord(AbstractDialogue dialogue) {
		Player player = null;
		Sequence sequence = dialogue.getSequence();
		WorldObject wso = sequence.getBinding(SoftSequenceBinding.PLAYER);
		
		// Retrieve the player
		if (wso == null) {
			AbstractEnvironment env = sequence.getEnvironment();
			if (env instanceof PlayerEnvironment) {
				PlayerEnvironment penv = (PlayerEnvironment) env;
				Collection<Player> players = penv.getPlayers();
				synchronized (players) {
					if (players.size() > 0)
						player = players.iterator().next();
				}
			}
		} else if (wso instanceof AbstractPlayerCharacter) {
			player = ((AbstractPlayerCharacter) wso).getPlayer();
		}
		
		// Retrieve the input from the player
		if (player != null) {
			Button button = Button.valueOf(input);
			ButtonInfo info = player.getInput().getButtonInfo(button);
			setTextureSet(info.getTextureSet());
		}
	}

}
